<template>
  <div class="wh100">
    <el-form label-position="top" :rules="rules" ref="form" :model="form" label-width="80px">
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="部门负责任人" prop="duty" :rules="[
      {
        required: true,
        message: '部门负责人名称不能为空',
        trigger: 'blur',
      },
    ]">
            <el-input :disabled="modeType == 2" v-model="form.duty"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="负责人电话" prop="mobile" required>
            <el-input :disabled="modeType == 2" type="tel" v-model="form.mobile" maxlength="11"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="部门名称" prop="name" :rules="[
      { required: true, message: '部门名称不能为空', trigger: 'blur' },
    ]">
            <el-input :disabled="modeType == 2" v-model="form.name"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="排序" prop="sort" :rules="[
      { required: true, message: '排序不能为空', trigger: 'blur' },
    ]">
            <el-input :disabled="modeType == 2" :min="1" v-model="form.sort" type="number"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="状态" prop="isStop" required>
            <el-row :gutter="24">
              <el-col :span="12">
                <el-switch :disabled="modeType == 2" v-model="form.isStop"></el-switch>
                <span class="left10">启用</span>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>

        <el-col :span="24" style="text-align: right" v-if="modeType !== 2">
          <el-form-item>
            <div class="btn">
              <div class="cancel">
                <el-button @click="cancelModel()">取消</el-button>
              </div>
              <div class="preservation">
                <el-button type="primary" :loading="loading" @click="onSubmit('form')">保存</el-button>
              </div>
            </div>

          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { addDept, editDept } from "@/api/dept";
export default {
  props: {
    modeType: {
      type: Number,
      default: 0,
    },
    organizationInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    const validateMobile = function (rule, value, callback) {
      let newValue = value.replace(/[^0-9]/gi, "");
      if (value !== newValue) {
        callback(new Error("请输入正确的手机号"));
      } else if (newValue.length !== 11) {
        callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    };
    return {
      form: {
        duty: "",
        isStop: false,
        mobile: "",
        name: "",
        pid: null,
        sort: null,
      },
      rules: {
        mobile: [
          {
            required: true,
            message: "部门负责人电话不能为空",
            trigger: "blur",
          },
          { validator: validateMobile.bind(this), trigger: "blur" },
        ],
      },
      loading: false,
    };
  },

  mounted() {
    if (this.modeType != 1) {
      this.form = JSON.parse(JSON.stringify(this.organizationInfo));
      this.form.isStop = this.form.isStop == 1 ? true : false;
    }
  },

  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.modeType == 1) {
            this.addDepts();
          } else if (this.modeType == 3) {
            this.editDepts();
          }
        }
      });
    },
    async addDepts() {
      let res = await addDept({
        duty: this.form.duty,
        isStop: this.form.isStop ? 1 : 0,
        mobile: this.form.mobile,
        name: this.form.name,
        pid: this.organizationInfo.id,
        sort: this.form.sort,
      });
      if (res.code == 200) {
        this.loading = false;
        this.$emit("onClose", { isClose: true });
      }
    },

    async editDepts() {
      let res = await editDept({
        duty: this.form.duty,
        id: this.organizationInfo.id,
        isStop: this.form.isStop ? 1 : 0,
        mobile: this.form.mobile,
        name: this.form.name,
        pid: this.organizationInfo.pid,
        sort: this.form.sort,
      });
      if (res.code == 200) {
        this.loading = false;
        this.$emit("onClose", { isClose: true });
      }
    },

    cancelModel() {
      this.$emit("onClose", {
        isClose: false,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.left10 {
  margin-left: 10px;
}

.btn {
  display: flex;
  gap: 20px;
  justify-content: center;
  margin-top: 110px;

  .cancel {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 0);
      color: rgba(92, 251, 255, 1);
    }
  }

  .preservation {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 1);
      color: rgba(23, 24, 26, 1);
    }
  }
}
</style>